import { _decorator, Component, Node, director, PhysicsSystem2D, Collider2D, Contact2DType, IPhysics2DContact, Sprite } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('pengzhuang')
export class pengzhuang extends Component {
    start() {
		
		// 碰撞检测开启
		PhysicsSystem2D.instance.enable = true
		let collider = this.getComponent(Collider2D);
		if (collider) {
			console.debug(collider)
			//参数1：碰撞类型
			//参数2：回调函数
			//参数3：信息会返回到哪个脚本（比如this就是这个，一般也是this，这里只做猜测，闲得无聊可以看源码）
			collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
			collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
			collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
			collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
		}
	}
	//回调函数
	onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
		// 只在两个碰撞体开始接触时被调用一次
		// console.debug(selfCollider.tag+'和'+otherCollider.tag+'碰上了')
		setTimeout(()=>{
			this.node.setPosition(200, -24.265,0)
		},3000)
    }
	onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
		// 只在两个碰撞体结束接触时被调用一次
		console.log('onEndContact');
	}
	onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
		// 每次将要处理碰撞体接触逻辑时被调用
		console.log('onPreSolve');
	}
	onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
		// 每次处理完碰撞体接触逻辑时被调用
		console.log('onPostSolve');
	}
    update(deltaTime: number) {
        this.node.setPosition(
			this.node.getPosition().x-100*deltaTime,
			this.node.getPosition().y
		)
    }
}

